// 权限组件
<template>
  <div class="power_component">
    <!-- 管理成员 -->
    <div class="power_component_manage">
      <p>成员</p>
      <manageMembers :treeNodeId="treeNodeId" @onRefresh="getTableData" />
    </div>
    <!-- 搜索条件 -->
    <div class="power_component_search">
      <el-input v-model="nickname" width="200" placeholder="搜索成员" clearable @keyup.enter="getTableData"/>
      <el-popover ref="sortPopoverRefs" placement="bottom" title="" width="160" trigger="click">
        <div class="people_box">
          <el-checkbox-group v-model="memberCheckList" @change="memberCheckChange">
            <el-checkbox :value="item.value" v-for="(item, index) in roleList" :key="index">
              {{ item.name }}
            </el-checkbox>
          </el-checkbox-group>
        </div>
        <template #reference>
          <span><i style="font-size: 12px" class="iconfont-el icon-arrow-down"></i> 权限</span>
        </template>
      </el-popover>
      <el-popover ref="sortPopoverRefs" placement="bottom" title="" width="160" trigger="click">
        <div class="deptBox">
          <el-checkbox-group v-model="dimensionalityCheckList" @change="memberCheckChange">
            <el-checkbox :value="item.value" v-for="(item, index) in dimensionalityList" :key="index">
              {{ item.label }}
            </el-checkbox>
          </el-checkbox-group>
        </div>
        <template #reference>
          <span> <i style="font-size: 12px" class="iconfont-el icon-arrow-down"></i> 维度</span>
        </template>
      </el-popover>
    </div>
    <!-- 表格 -->
    <el-table :data="tableData" style="width: 100%" height="100%" v-loading="loading">
      <el-table-column prop="name" label="姓名">
        <template #default="scope">
          <div class="table_NameBox">
            <!-- <img v-if="scope.row.type == '3'" style="background: #3662EC;" src="@/assets/images/jues.png" alt="" />
                  <img v-if="scope.row.type == '2'" style="background: #FE4D4F;" src="@/assets/images/gwei.png" alt="" />
                  <img v-if="scope.row.type == '1'" style="background: #07b9b9;" src="@/assets/images/bumeng.png" alt="" /> -->
            <svg-icon v-if="scope.row.type == '1'" icon-class="deptPower" style="font-size: 25px;margin-right: 5px;"></svg-icon>
            <svg-icon v-if="scope.row.type == '2'" icon-class="rolePower" style="font-size: 25px;margin-right: 5px;"></svg-icon>
            <svg-icon v-if="scope.row.type == '3'" icon-class="jobPower" style="font-size: 20px;margin-right: 5px;"></svg-icon>
            <div class="avatar" v-if="scope.row.type == '0'">
              <div>{{ scope.row.name.slice(0, 1) }}</div>
            </div>
            <div class="table_Name" v-if="scope.row.id != '000'">
              <p>{{ scope.row.name }}</p>
              <span v-if="scope.row.isExtend == 0">由【{{ scope.row.creator ?? '--' }}】添加</span>
              <span v-else>继承权限</span>
            </div>
            <div class="table_Name" v-else>
              <span class="table_Name_span">{{ scope.row.name }}</span>
              <span class="table_Name_syz">所有者</span>
            </div>
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="params" label="权限">
        <template #default="scope">
          <div class="temp_box" v-if="scope.row.id != '000'">
            <div class="role_box">
              <el-select
                v-model="scope.row.params"
                placeholder="请选择"
                popper-class="roleType_popper_class_enterprise"
                @change="roleTypeChange($event, scope.row)"
                style="width: 140px"
              >
                <el-option v-for="item in roleList" :key="item.value" :value="item.value" :label="item.name" :disabled="item.disabled">
                  <div class="role_item_box">
                    <div class="name">
                      <span>{{ item.name }}</span>
                      <el-icon v-if="scope.row.params == item.value"><Check /></el-icon>
                    </div>
                    <div class="describe">{{ item.describe }}</div>
                  </div>
                </el-option>
              </el-select>
            </div>
            <div class="icon_box" v-if="scope.row.roleType != '0'" @click="deleteUserClick([scope.row])">
              <el-icon><Close /></el-icon>
            </div>
          </div>
          <div v-else style="color: #999">管理员</div>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-pagination
      @size-change="getSizeChange"
      @current-change="getPageChange"
      :current-page="commonParams.page"
      :page-sizes="[10, 20, 30, 40]"
      :page-size="commonParams.size"
      layout="total, prev, pager, next, sizes"
      :total="commonParams.total"
    />
  </div>
</template>
<script setup lang="ts">
// 自定义组件
import manageMembers from './manageMembers.vue';
// 接口
import { apiEnterprisePermSelectRoler, apiEnterprisePermUpdateRoler,apiEnterprisePermDeleteRoler } from '@/api/enterpriseDoc/enterpriseListApi';
import { ref, reactive } from 'vue';
import { ElMessageBox,ElMessage } from 'element-plus';
import { roleList_enterprise } from '@/utils/roleList.js';
const props = defineProps({
  treeNodeId: {
    type: Number,
    default: 0
  },
  createBy: {
    type: String,
    default: ""
  }
});
const nickname = ref<string>('');
const roleList = reactive(roleList_enterprise); // 权限筛选列表
const memberCheckList = ref<string[]>([]); // 权限筛选选择值
const dimensionalityList = ref<any[]>([
  { value: '0', label: '用户' },
  { value: '1', label: '部门' },
  { value: '2', label: '角色' },
  { value: '3', label: '岗位' }
]); // 维度筛选列表
const dimensionalityCheckList = ref<string[]>([]); // 维度筛选选择值
const tableData = ref<any[]>([]); // 表格数据
const commonParams = reactive({
  page: 1,
  size: 10,
  total: 0
});
// 加载中
const loading = ref<boolean>(false);
// 分页
const getSizeChange = (size: number) => {
  commonParams.size = size;
  getTableData();
};
// 页码
const getPageChange = (page: number) => {
  commonParams.page = page;
  getTableData();
};
// 获取表格数据
const getTableData = async () => {
  let params = {
    page: commonParams.page - 1,
    size: commonParams.size,
    nickname: nickname.value,
    sort: 'id,asc',
    treeNodeId: props.treeNodeId,
    params: JSON.stringify(memberCheckList.value),
    types: JSON.stringify(dimensionalityCheckList.value)
  };
  loading.value = true;
  let res: any = await apiEnterprisePermSelectRoler(params);
  loading.value = false;
  if (res.code === 200) {
    commonParams.total = res.data.total;
    tableData.value = res.data.rows;
    tableData.value.unshift({ name: props.createBy, id: "000", type: "0" });
  }
};
// 权限筛选
const memberCheckChange = () =>{
    commonParams.page = 1;
    getTableData();
}
// 修改权限
const roleTypeChange = async (value: string, row: any) => {
    let parmas = {
        ids: [row.id],
        permCodes: row.params,
        treeNodeId: props.treeNodeId,
    }
    let res = await apiEnterprisePermUpdateRoler(parmas);
    if(res.code === 200){
        ElMessage.success('修改成功');
    }
};
// 权限权限
const deleteUserClick = (row: any) => {
  ElMessageBox.confirm('此操作将删除1位成员, 是否继续?','提示',{type:'warning'}).then(() => {
    let params = {
      ids: row.map((item: any) => item.id),
      treeNodeId: props.treeNodeId
    };
    apiEnterprisePermDeleteRoler(params).then((res: any) => {
      if (res.code === 200) {
        ElMessage.success('删除成功');
        getTableData();
      }
    });
  }).catch(() => {});
};
defineExpose({
  getTableData
});
</script>
<style scoped lang='scss'>
p {
  margin: 0;
}
:deep(.el-checkbox-group) {
  display: flex;
  flex-direction: column;
}
.power_component {
  height: 100%;
  display: flex;
  flex-direction: column;
  .power_component_manage {
    display: flex;
    justify-content: space-between;
    align-items: center;
  }
  .power_component_search {
    display: flex;
    align-items: center;
    .el-tooltip__trigger {
      cursor: pointer;
      width: 60px;
      height: 30px;
      text-align: center;
      line-height: 30px;
      border-radius: 4px;
      &:hover {
        color: #409eff;
        background-color: rgba(#63affa, 0.1);
      }
    }
    .el-input {
      width: 300px;
      margin-right: 20px;
    }
  }
  .el-table {
    flex: 1;
    margin-top: 10px;
  }
  .el-pagination {
    margin-top: 10px;
  }
}
.table_NameBox {
  display: flex;
  align-items: center;

  .avatar {
    width: 24px;
    height: 24px;
    border-radius: 12px;
    display: flex;
    align-items: center;
    justify-content: center;
    color: #fff;
    font-size: 12px;
    background-color: #56abfb;
    margin-right: 4px;

    div {
      margin-top: 2px;
      margin-left: 1px;
      // transform: scale(0.9, 0.9);
    }
  }

  img {
    padding: 2px;
    width: 22px;
    height: 22px;
    margin-right: 5px;
    border-radius: 15px;
  }

  .table_Name {
    margin-left: 5px;

    p {
      margin: 0;
    }

    span {
      color: #999;
    }

    .table_Name_span {
      color: black;
    }

    .table_Name_syz {
      border: 1px solid #83acff;
      padding: 2px 5px;
      border-radius: 5px;
      margin-left: 5px;
      color: #83acff;
    }
  }
}
.temp_box {
  display: flex;
  justify-content: space-between;
  align-items: center;
  .role_box {
    width: 100%;
    :deep(.el-input__inner) {
      width: 100px;
      border: 0;
    }
    .all_role_text {
      margin-left: 15px;
      color: #bbbfc4;
      font-size: 13px;
    }
  }
  .icon_box {
    font-size: 16px;
    cursor: pointer;
    visibility: hidden;

    :hover {
      color: #f56c6c;
    }
  }
}
.el-select-dropdown__item {
  height: auto;
  p {
    margin: 0;
  }
  line-height: 20px;
}
.role_item_box {
  padding: 10px 0;
  .name {
    display: flex;
    justify-content: space-between;
    font-weight: bold;
  }
  .describe {
    font-size: 11px;
  }
}
:deep(.el-table__body tr:hover .icon_box) {
  visibility: visible;
}
</style>